<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/10/21
 * Time: 6:12
 */

/*
 * 解决方案一
 * */
class Solution_01
{
    public function twoSum(array $numArray, int $target):array {
        $length = count($numArray);
        for($i=0;$i<=$length-1;$i++){
            for($j=$i+1;$j<=$length-1;$j++){
                if($numArray[$i]+$numArray[$j] == $target){
                    return [$i,$j];
                }
            }
        }
        return [];
    }
}
$numArray = [3,2,4];
$target = 6;
$TwoSum = new Solution_01();
print_r($TwoSum->twoSum($numArray,$target));

/*
 *
 * 解决方案二
 * */
class Solution_02{
    public function twoSum(array $numArray, int $target):array {
        $tmpArray = [];
        foreach ($numArray as $key =>$val){
            $tmp = $target - $val;
            if(isset($tmpArray[$tmp])){
                return [$tmpArray[$tmp],$key];
            }
            $tmpArray[$val] = $key;
        }
        return [];
    }
}
$numArray = [3,2,4];
$target = 6;
$TwoSum = new Solution_02();
print_r($TwoSum->twoSum($numArray,$target));